package com.aurora.source.center.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.aurora.source.R;
import com.aurora.source.base.BaseRecyclerAdapter;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.center.adapter.BatchListAdapter;
import com.aurora.source.center.presenter.BatchSelectPresenter;
import com.aurora.source.center.view.IBatchSelectView;
import com.aurora.source.home.bean.BatchManagementBean;
import com.aurora.source.home.presenter.BatchManagementPresenter;
import com.aurora.source.home.view.IBatchManagementView;
import com.aurora.source.util.SPUtils;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: ww
 * @description: 上级批次选择
 * @date: 2018/10/8
 */
public class BatchSelectActivity extends BaseWhiteToolbarActivity<BatchSelectPresenter> implements IBatchSelectView , IBatchManagementView {

    BatchListAdapter batchListAdapter;
    @BindView(R.id.rv_batch)
    RecyclerView rvBatch;
    BatchManagementPresenter batchManagementPresenter;
    private String number;//其他流程选择批次需要传number，当前序号
    @Override
    public BatchSelectPresenter attach() {
        batchManagementPresenter = new BatchManagementPresenter(this);
        return new BatchSelectPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_batch_select;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        number = getIntent().getStringExtra("number");
        batchListAdapter = new BatchListAdapter();
        batchListAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<BatchManagementBean.BatchListBean>() {
            @Override
            public void onClick(int position, BatchManagementBean.BatchListBean data) {
                Intent intent = new Intent();
                intent.putExtra("name",data.getBatchTitle());
                intent.putExtra("number",data.getBatchNo());
                setResult(RESULT_OK,intent);
                hideSoftInput();
                finish();
            }
        });
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        simpleToolbar.setMainTitle("批次选择");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
        rvBatch.setLayoutManager(new LinearLayoutManager(this));
        rvBatch.setAdapter(batchListAdapter);
    }

    @Override
    public void doBusiness() {
        if(TextUtils.isEmpty(number)){
            basePresenter.getBatchList(userId);
        }else {
            params = new HashMap<>();
            params.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
            params.put("number", number);
            batchManagementPresenter.batchManagement(this, params,"1");
        }

    }

    @Override
    public void onGetBatchListSuccess(List<BatchManagementBean.BatchListBean> batchList) {
        batchListAdapter.setDataList(batchList);
    }

    @Override
    public void onGetBatchFail(String msg) {
        showToast(msg);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onBatchManagementSuccess(BatchManagementBean data) {
        batchListAdapter.setDataList(data.getBatchList());
    }

    @Override
    public void onBatchManagementFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onChangeSuccess(String msg) {

    }

    @Override
    public void onChangeFail(String msg) {

    }
}
